package com.ayf.payment.game.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.GameInfoDTO;
import com.ayf.payment.game.api.dto.GameInfoGiftDTO;
import com.ayf.payment.game.api.dto.criteria.GameInfoCriteria;
import com.ayf.payment.game.api.dto.criteria.GameInfoPageCriteria;
import com.ayf.payment.game.api.entity.GameInfo;
import com.ayf.payment.game.api.entity.GameInfoGift;
import com.ayf.payment.game.api.entity.GameInfoId;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.GameInfoGiftMapper;
import com.ayf.payment.game.api.mapper.GameInfoIdMapper;
import com.ayf.payment.game.api.mapper.GameInfoMapper;
import com.ayf.payment.game.api.service.GameInfoService;
import com.ayf.payment.game.api.utils.DateUtils;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GameInfoServiceImpl implements GameInfoService {

    @Resource
    private GameInfoMapper gameInfoMapper;

    @Resource
    GameInfoIdMapper gameInfoIdMapper;
    @Resource
    private GameInfoGiftMapper gameInfoGiftMapper;

    @Override
    public boolean insert(GameInfoCriteria gameInfo) {
        if (gameInfo == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        GameInfoId gameInfoId = new GameInfoId();
        gameInfoIdMapper.insert(gameInfoId);
        gameInfo.setId(String.valueOf(gameInfoId.getId()));
        GameInfo info = BeanConvertUtils.convert(gameInfo, GameInfo.class);
        gameInfoMapper.insertSelective(info);
        if (info.getGiftStatus() == 1) {
            checkGiftInfo(gameInfo);
            for (GameInfoGiftDTO giftDTO : gameInfo.getTimeList()) {
                GameInfoGift gameInfoGift = GameInfoGift.builder()
                        .gameInfoId(gameInfo.getId())
                        .total(gameInfo.getTotal())
                        .gameCurrency(gameInfo.getGameCurrency())
                        .amount(gameInfo.getAmount())
                        .used(0)
                        .startTime(giftDTO.getStartTime())
                        .endTime(giftDTO.getEndTime())
                        .createTimeIndex(DateUtils.dateToInteger(new Date()))
                        .build();
                gameInfoGiftMapper.insertSelective(gameInfoGift);
            }
        }
        return true;
    }

    @Override
    public boolean del(String id, Integer merchantId) {
        Example example = new Example(GameInfo.class);
        example.createCriteria().andEqualTo("id", id).andEqualTo("merchantId", merchantId);
        GameInfo gameInfo = new GameInfo();
        gameInfo.setStatus(-1);
        GameInfoGift gameInfoGift = GameInfoGift.builder().gameInfoId(id).build();
        gameInfoGiftMapper.delete(gameInfoGift);
        return gameInfoMapper.updateByExampleSelective(gameInfo, example) > 0;
    }

    private GameInfo selectGame(String name, Integer merchantId) {
        GameInfo gameInfo = new GameInfo();
        gameInfo.setMerchantId(merchantId);
        gameInfo.setGameInfoName(name);
        return gameInfoMapper.selectOne(gameInfo);
    }

    @Override
    public GameInfoDTO selectOne(String id) {
        if (StringUtils.isBlank(id)) {
            throw new ParameterException("游戏信息id不能为空");
        }
        return gameInfoMapper.selectGameInfoById(id);
    }

    @Override
    public List<GameInfoDTO> selectPage(GameInfoPageCriteria gameInfoCriteria) {
        if (gameInfoCriteria == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        GameInfo gameInfo = BeanConvertUtils.convert(gameInfoCriteria, GameInfo.class);
        return gameInfoMapper.selectGameInfo(gameInfo);
    }

    @Override
    public List<GameInfoDTO> select(GameInfoCriteria gameInfoCriteria) {
        if (gameInfoCriteria == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        GameInfo gameInfo = BeanConvertUtils.convert(gameInfoCriteria, GameInfo.class);
        gameInfo.setIdList(gameInfoCriteria.getIdList());
        return gameInfoMapper.selectGameInfo(gameInfo);
    }

    @Override
    public boolean update(GameInfoCriteria gameInfo) {
        if (gameInfo == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        GameInfo info = BeanConvertUtils.convert(gameInfo, GameInfo.class);
        gameInfoMapper.updateByPrimaryKeySelective(info);
        if (gameInfo.getGiftStatus() == 1) {
            checkGiftInfo(gameInfo);
            gameInfoGiftMapper.delete(GameInfoGift.builder().gameInfoId(info.getId()).build());
            for (GameInfoGiftDTO giftDTO : gameInfo.getTimeList()) {
                GameInfoGift gameInfoGift = GameInfoGift.builder()
                        .gameInfoId(info.getId())
                        .total(gameInfo.getTotal())
                        .gameCurrency(gameInfo.getGameCurrency())
                        .amount(gameInfo.getAmount())
                        .startTime(giftDTO.getStartTime())
                        .endTime(giftDTO.getEndTime())
                        .createTimeIndex(DateUtils.dateToInteger(new Date()))
                        .build();
                if (giftDTO.getId() == null) {
                    gameInfoGift.setUsed(0);
                } else {
                    gameInfoGift.setId(giftDTO.getId());
                    gameInfoGift.setUsed(giftDTO.getUsed());
                }
                gameInfoGiftMapper.insertSelective(gameInfoGift);
            }
        }
        return true;
    }

    private void checkGiftInfo(GameInfoCriteria gameInfo) {
        if (gameInfo.getTotal() == null || gameInfo.getTotal() <= 0) {
            throw new ParameterException("总量必须大于0");
        }
        if (gameInfo.getAmount() == null || gameInfo.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new ParameterException("礼包金额必须大于0");
        }
        List<GameInfoGiftDTO> timeList = gameInfo.getTimeList();
        if (CollUtil.isEmpty(timeList)) {
            throw new ParameterException("至少选择一个时间段");
        }
    }

    @Override
    public List<GameInfoDTO> list(Integer merchantId) {
        GameInfo gameInfo = new GameInfo();
        gameInfo.setMerchantId(merchantId);
        return BeanConvertUtils.convert(gameInfoMapper.select(gameInfo), GameInfoDTO.class);
    }

    @Override
    public GameInfo getById(String id) {
        return gameInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateSort(GameInfoCriteria criteria) {
        if (StringUtils.isEmpty(criteria.getId())){
            throw new ParameterException("ID不能为空");
        }
        if (criteria.getPartitionSort() == null || criteria.getPartitionSort().compareTo(Integer.valueOf(0))<0){
            throw new ParameterException("序号不能为空值且不能小于0");
        }
        GameInfo gameInfo = new GameInfo();
        gameInfo.setLastUpdateTime(DateUtils.getNowDate());
        gameInfo.setPartitionSort(criteria.getPartitionSort());
        gameInfo.setId(criteria.getId());
        gameInfoMapper.updateByPrimaryKeySelective(gameInfo);
    }
}

